package com.shop.operator.controller;

import java.util.HashMap;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.domain.PageResult;
import com.shop.domain.Result;
import com.shop.model.Brand;
import com.shop.operator.service.impl.BrandServiceBean;

@Controller
@RequestMapping("/brand")
public class BrandAction {
	@Resource(name="brandServiceBean")
	BrandServiceBean brandServiceBean;
	
/**
 * 为了访问brand.jsp 进行转发操作
 * @param request
 * @return
 * @throws Exception
 */
	@RequestMapping("/firstLoad")
	public String findAllBrands(HttpServletRequest request) throws Exception{
		return "forward:/WEB-INF/operator/admin/brand.jsp";
	}
	
/**
 * 查找符合条件的总记录条数 和 页数	 查找全部与条件查找共用
 * 
 * @param request
 * @param pageNum
 * @param pageSize
 * @param name
 * @return
 * @throws Exception
 */
	@RequestMapping("/findPage")
	@ResponseBody
	public PageResult findPage(HttpServletRequest request,Integer pageNum,Integer pageSize,String name) throws Exception{
			PageResult findPage = brandServiceBean.findPage(pageNum, pageSize, name);
			return findPage;
	}
/**
 * 查找该页的全部记录  全部查找与条件查找共用	
 * 因为当时没有发现可以直接从PageResult中获取记录条数
 * 所以又写了个获取当前页记录的方法
 * 在jsp页面 先访问findPage 显示 分页栏信息
 * 然后再访问findByPage 显示当页信息
 * @param request
 * @param pageNum 当前页号
 * @param pageSize 当前页大小
 * @param name	可进行模糊查找的品牌名
 * @return
 * @throws Exception
 */

	@RequestMapping("/findByPage")
	@ResponseBody
	public List<Brand> findBrand(HttpServletRequest request,Integer pageNum,Integer pageSize,String name) throws Exception{
			Map<String,String> map = new HashMap<>();
			map.put("begin",""+(long)((pageNum-1)*pageSize));
			map.put("end", ""+(long)pageSize);
			if(name==null){
				name="";
			}
			map.put("name",name);
			List<Brand> brands = brandServiceBean.findByPage(map);
			return brands;
	}
	
/**
 * 更新品牌数据
 * @param request
 * @param brand 品牌对象
 * @param response
 * @return 返回新对象 用于异步更新
 */
	
	@RequestMapping("/update")
	@ResponseBody
	public Brand update(HttpServletRequest request, Brand brand ,HttpServletResponse response ){
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		try {
			brandServiceBean.updateById(brand);
			return brand;
		} catch (Exception e) {
			return null;
		}
		
	}
	
	
/**
 * 删除品牌
 * @param request
 * @param response
 * @param delChecks 前端传过来的id序列 直接注入了 List
 * @return
 * @throws Exception
 */
	@RequestMapping("/delete")
	@ResponseBody
	public Result delete(HttpServletRequest request,HttpServletResponse response, @RequestParam("delCheck") List<Long> delChecks){
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		
		try {
			brandServiceBean.deleteByIds(delChecks);
			return new Result("删除成功:)");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result("删除失败:(");
		}
	}
	
/**
 * 插入
 * @param request
 * @param response
 * @param brand
 * @return
 * @throws Exception
 */
	@RequestMapping("/insert")
	@ResponseBody
	public Result insert(HttpServletRequest request,HttpServletResponse response, Brand brand){
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		try {
			brandServiceBean.insert(brand);
			return new Result("创建成功:)");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result("创建失败:)");
		}
		
	}
	
	
}
